﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Model.Sys;
using System.Web;
using System.Web.Security;
using Sub.Function;


namespace BusinessRule.Sys
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class Sys : ISys
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
        /// <summary>
        /// implement user login
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <param name="SessionId"></param>
        /// <returns></returns>
        public bool ValidateUser(string UserName, string Password, string SessionId)
        {
            using (DataSys db = new DataSys())
            {
                var ret = Membership.ValidateUser(UserName, Password);
                if (ret)
                {
                    var userID = db.aspnet_Users.FirstOrDefault(p => p.UserName == UserName).UserId;
                    var loginItem = new sys_Login()
                    {
                        ID = Guid.NewGuid(),
                        LoginDate = DateTime.Now,
                        SessionId = SessionId,
                        UserID = userID,
                        UserName = UserName
                    };
                    db.sys_Logins.InsertOnSubmit(loginItem);
                }

                db.SubmitChanges();
                return ret;
            }

        }
        /// <summary>
        /// execute sql query
        /// </summary>
        /// <param name="SQL"></param>
        /// <param name="action"></param>
        public void SQL_ExecuteQuery(string SQL, ref ActionService action)
        {
            using (DataSys db = new DataSys())
            {
                /*
                var permit = db.GetPermitionEntry(action.Token); if (action.Token.TimeOut) { return; }
                if (SQL.ToLower().IndexOf("update ") > -1)
                {
                    if (!permit.AllowUpdate)
                    {
                        action.ErrorType = eActionErrorType.AccessDenife;
                        return;
                    }
                }
                if (SQL.ToLower().IndexOf("delete ") > -1)
                {
                    if (!permit.AllowDelete)
                    {
                        action.ErrorType = eActionErrorType.AccessDenife;
                        return;
                    }
                }
                if (SQL.ToLower().IndexOf("insert ") > -1)
                {
                    if (!permit.AllowInsert)
                    {
                        action.ErrorType = eActionErrorType.AccessDenife;
                        return;
                    }
                }
                if (SQL.ToLower().IndexOf("alter ") > -1)
                {
                    action.ErrorType = eActionErrorType.AccessDenife;
                    return;
                }
                if (SQL.ToLower().IndexOf("create ") > -1)
                {
                    action.ErrorType = eActionErrorType.AccessDenife;
                    return;
                }
                if (SQL.ToLower().IndexOf("drop ") > -1)
                {
                    action.ErrorType = eActionErrorType.AccessDenife;
                    return;
                }
                 * */
                try
                {
                    db.ExecuteQuery<object>(SQL);
                }
                catch (System.Data.Linq.DuplicateKeyException ex)
                {
                    action.ErrorType = EnumType.eActionErrorType.System;
                    action.ErrorMessage = ex.Message;
                    action.DataErrorType = EnumType.eActionErrorType.UniqueKey;
                    action.DataErrorField = ex.Object.ToString();

                }
                catch (System.Exception ex)
                {
                    action.ErrorType = EnumType.eActionErrorType.System;
                    action.ErrorMessage = ex.Message;
                }
            }
        }
    }
}
